package com.th.therp.controller;

import com.th.therp.exception.BaseException;
import com.th.therp.pojo.emtity.MaterialBrand;
import com.th.therp.pojo.result.Result;
import com.th.therp.service.MaterialBrandService;
import jakarta.validation.Valid;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品品牌请求控制器
 */
@Log4j2
@RestController
@RequestMapping("/materialBrand")
public class MaterialBrandController {

    @Autowired
    private MaterialBrandService materialBrandService;

    //查找全部商品品牌
    @ResponseBody
    @GetMapping("/page")
    public Result<List<MaterialBrand>> list(){
        log.info("查找全部商品品牌");
        List<MaterialBrand> materialBrandList = materialBrandService.list();
        return Result.success(materialBrandList);
    }

    //新增商品品牌
    @ResponseBody
    @PostMapping()
    public Result add(@Valid @RequestBody MaterialBrand materialBrand, BindingResult bindingResult){
        log.info("新增商品品牌:{}",materialBrand);
        if (bindingResult.hasErrors()){
            throw new BaseException("格式不正确");
        }
        materialBrandService.add(materialBrand);
        return Result.success();
    }

    //修改商品品牌
    @ResponseBody
    @PutMapping()
    public Result update(@Valid @RequestBody MaterialBrand materialBrand, BindingResult bindingResult){
        log.info("修改商品品牌:{}",materialBrand);
        if (bindingResult.hasErrors()){
            throw new BaseException("格式不正确");
        }
        materialBrandService.update(materialBrand);
        return Result.success();
    }

    //删除商品品牌
    @ResponseBody
    @DeleteMapping()
    public Result delete(@RequestParam List<Integer> ids){
        log.info("删除商品品牌:{}",ids);
        materialBrandService.delete(ids);
        return Result.success();
    }

}
